package com.huicoo.forestmanager.ui.patrol.forestCampTask.presenter;


import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.huicoo.forestmanager.network.bean.patrol.AttachmentEntity;
import com.huicoo.forestmanager.network.bean.patrol.EventTypeBean;
import com.huicoo.forestmanager.network.bean.patrol.RecordDetailsIssuesListBean;
import com.huicoo.forestmanager.network.bean.patrol.ReportEventBean;
import com.huicoo.forestmanager.network.bean.patrol.UploadFileBean;
import com.huicoo.forestmanager.others.Constants;
import com.huicoo.forestmanager.ui.patrol.forestCampTask.contract.ForestCampTaskReportEventContract;
import com.huicoo.forestmanager.ui.patrol.forestCampTask.model.ForestCampTaskReportEventModel;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.LogUtils;
import com.huicoo.forestmanager.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2019/4/10	    Administrator
 * 事件上报的presenter
 */
public class ForestCampTaskReportEventPresenter implements ForestCampTaskReportEventContract.Presenter {
    private ForestCampTaskReportEventContract.View view;
    private ForestCampTaskReportEventContract.Model model;

    public ForestCampTaskReportEventPresenter(ForestCampTaskReportEventContract.View view) {
        this.view = view;
        this.model = new ForestCampTaskReportEventModel();
    }

    @Override
    public void getEventType(HashMap<String, String> params) {
        Call<EventTypeBean> call = model.getEventType(params);
        call.enqueue(new Callback<EventTypeBean>() {
            @Override
            public void onResponse(@NonNull Call<EventTypeBean> call, @NonNull Response<EventTypeBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo get event type result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        List<EventTypeBean.EventTypeData> typeList = response.body().getData();
                        //防止空指针
                        if (typeList == null) {
                            typeList = new ArrayList<>();
                        }
                        if (view != null) {
                            view.getEventTypeSuccess(typeList);
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<EventTypeBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void uploadFile(HashMap<String, String> params, AttachmentEntity attachment) {
        Call<UploadFileBean> call = model.uploadFile(params, attachment);
        call.enqueue(new Callback<UploadFileBean>() {
            @Override
            public void onResponse(@NonNull Call<UploadFileBean> call, @NonNull Response<UploadFileBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo upload file result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        List<UploadFileBean.UploadFileData> Data = response.body().getData();
                        if (Data != null && !Data.isEmpty() && Data.get(0) != null && Data.get(0).IsSuccess) {
                            if (view != null) {
                                view.uploadFileSuccess(Data.get(0), attachment.attachmentType, attachment.during);
                            }
                        } else {
                            if (view != null) {
                                view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                            }
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<UploadFileBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                LogUtils.v("gogogo on failure = " + t.getMessage());
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void reportEvent(HashMap<String, String> params) {
        Call<ReportEventBean> call = model.reportEvent(params);
        call.enqueue(new Callback<ReportEventBean>() {
            @Override
            public void onResponse(@NonNull Call<ReportEventBean> call, @NonNull Response<ReportEventBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo report event result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        ReportEventBean.ReportEventData Data = response.body().getData();
                        if (Data != null && Data.EventID > 0) {
                            if (view != null) {
                                view.reportEventSuccess();
                            }
                        } else {
                            if (view != null) {
                                view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                            }
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<ReportEventBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                LogUtils.v("gogogo on failure = " + t.getMessage());
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }

    @Override
    public void getMyReportEvent(HashMap<String, String> params) {
        Call<RecordDetailsIssuesListBean> call = model.getMyReportEvent(params);
        call.enqueue(new Callback<RecordDetailsIssuesListBean>() {
            @Override
            public void onResponse(@NonNull Call<RecordDetailsIssuesListBean> call, @NonNull Response<RecordDetailsIssuesListBean> response) {
                try {
                    //数据请求成功
                    LogUtils.v("gogogo get my report event result = " + JsonUtils.toJson(response.body()));
                    if (response.body() == null) {
                        //数据请求失败
                        if (view != null) {
                            view.fail("请求结果为空");
                        }
                        return;
                    }
                    if (TextUtils.equals(response.body().getCode(), "200")) {
                        RecordDetailsIssuesListBean.Data Data = response.body().getData();
                        if (Data != null && Data.getRows() != null) {
                            if (view != null) {
                                view.getMyReportEventSuccess(Data.getRows());
                            }
                        } else {
                            if (view != null) {
                                view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                            }
                        }
                    } else {
                        if (view != null) {
                            view.fail(TextUtils.isEmpty(response.body().getErrorMsg()) ? "请求失败，请重试" : response.body().getErrorMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(@NonNull Call<RecordDetailsIssuesListBean> call, @NonNull Throwable t) {
                //数据请求失败
                t.printStackTrace();
                if (view != null) {
                    view.fail("请求失败，请重试");
                }
            }
        });
    }


    @Override
    public void destroy() {
        view = null;
    }

}
